package com.gxitsky.utils;

public class HexUtil {


	private final static char[] mChars = "0123456789ABCDEF".toCharArray();

    /**
     * @param keyIndex
     * @param num
     *            表示十六进制字符的个数
     * @return
     */
    public static String leftAdd0Tostring(String keyIndex, int num) {
        if (keyIndex.length() > num) {
            return keyIndex.substring(keyIndex.length() - num);
        } else if (keyIndex.length() == num) {
            return keyIndex;
        } else {
            StringBuffer sb = new StringBuffer();
            int add0Num = num - keyIndex.length();
            for (int i = 0; i < add0Num; i++) {
                sb.append(mChars[0]);
            }
            for (int i = 0; i < keyIndex.length(); i++) {
                sb.append(keyIndex.charAt(i));
            }
            return sb.toString();
        }
    }

    /**
     * @param accessPwd
     * @return
     */
    public static String stringFormatter(String accessPwd, int num) {
        StringBuffer sb = new StringBuffer();
        byte[] bs = accessPwd.getBytes();
        int add0Num = num - bs.length * 2;
        for (int i = 0; i < add0Num; i++) {
            sb.append(mChars[0]);
        }
        for (int i = 0; i < bs.length; i++) {
            sb.append(mChars[(bs[i] & 0xFF) >> 4]);
            sb.append(mChars[bs[i] & 0x0F]);
        }
        return sb.toString();
    }

    public static byte convHexCharToByte(char leftChar, char rightChar) {
        byte leftByte = (byte) "0123456789ABCDEF".indexOf(leftChar);
        byte rightByte = (byte) "0123456789ABCDEF".indexOf(rightChar);
        return (byte) ((leftByte << 4) | rightByte);
    }

    /**
     * @return
     */
    public static byte[] hex2byte(String hexStr) {
        if (hexStr == null || hexStr.equals("")) {
            return null;
        }
        hexStr = hexStr.toUpperCase();
        int length = hexStr.length() / 2;
        char[] hexChars = hexStr.toCharArray();
        byte[] retBytes = new byte[length];

        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            retBytes[i] = convHexCharToByte(hexChars[pos], hexChars[pos + 1]);
        }
        return retBytes;
    }

    /**
     * @param dataLen
     * @return
     */
    public static int bytesToInt(byte[] dataLen) {
        int sum = 0;
        int length = dataLen.length;
        for (int i = 0; i < length; i++) {
            sum *= 256;
            sum += dataLen[i] & 0x0f;
            sum += dataLen[i] & 0xf0;
        }
        return sum;
    }

    /**
     * @param packet
     * @return 0x65 0x34 ---> 6534
     */
    public static String bytes2HexString(byte[] packet, int startPos, boolean isLowerCase) {
        StringBuffer sb = new StringBuffer();
        String hex = null;
        for (int i = startPos; i < packet.length; i++) {
        	if(isLowerCase)
        		hex = Integer.toHexString(packet[i] & 0xFF).toLowerCase();
        	else
        		hex = Integer.toHexString(packet[i] & 0xFF).toUpperCase();
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex);
        }
        return sb.toString();
    }

    /**
     * @return
     */
    public static int hex2int(String hexStr) {
        if (hexStr == null || hexStr.equals("")) {
            return -1;
        }
        int sum = 0;
        hexStr = hexStr.toUpperCase();
        int length = hexStr.length();
        for(int i = 0; i < length; i++) {
            sum *= 16;
            sum += "0123456789ABCDEF".indexOf(hexStr.charAt(i));
        }
        return sum;
    }
}
